<template>
	<view class="login" :style="{ paddingTop: navHeight }">
		<view class="customNav" :style="{ height: navHeight }">
			<view class="navTitle">账号登录</view>
			<view class="registerText">注册</view>
		</view>
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体表单 -->
			<view class="main">
				<wInput v-model="phoneData" inputImg="/static/login/login-user.png" type="text" maxlength="11" placeholder="请输入账号/手机号"></wInput>
				<wInput v-model="passData" inputImg="/static/login/login-pwd.png" type="password" maxlength="11" placeholder="请输入密码"></wInput>
			</view>
			<view class="forgetPwd">
				<text class="pwdText">忘记密码?</text>
			</view>
			<wButton text="登 录" bgColor="#F33632" :rotate="isRotate" @click.native="startLogin()"></wButton>
			
			<!-- 其他登录 -->
			<view class="promptText">其他登录方式</view>
			<view class="other_login cuIcon">
				<view class="login_icon">
					<image src="/static/login/login-qq.png" @tap="login_qq"></image>
				</view>
				<view class="login_icon">
					<image src="/static/login/login-wx.png" @tap="login_weixin"></image>
				</view>
			</view>

			<!-- 底部信息 -->
			<view class="footer">
				<text>登录即代表阅读并同意</text>
				<navigator class="text_red" url="" open-type="navigate">服务协议</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	var _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button

	export default {
		data() {
			return {
				logoImage: '/static/login/login-home.png',
				phoneData: '', //用户/电话
				passData: '', //密码
				isRotate: false, //是否加载旋转
				navHeight: 0,
			};
		},
		components: {
			wInput,
			wButton,
		},
		onLoad() {
			uni.getSystemInfo({
				success: res => {
					this.navHeight = `${res.statusBarHeight + 46}px`;
				}
			});
		},
		mounted() {
			_this = this;
			//this.isLogin();
		},
		methods: {
			isLogin() {
				//判断缓存中是否登录过，直接登录
				// try {
				// 	const value = uni.getStorageSync('setUserData');
				// 	if (value) {
				// 		//有登录信息
				// 		console.log("已登录用户：",value);
				// 		_this.$store.dispatch("setUserData",value); //存入状态
				// 		uni.reLaunch({
				// 			url: '../../../pages/index',
				// 		});
				// 	}
				// } catch (e) {
				// 	// error
				// }
			},
			startLogin() {
				//登录
				if (this.isRotate) {
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.phoneData.length == "") {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '用户名不能为空'
					});
					return;
				}
				if (this.passData.length < 5) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '密码不正确'
					});
					return;
				}

				_this.isRotate = true
				setTimeout(function() {
					_this.isRotate = false
				}, 3000)
				uni.showLoading({
					title: '登录中'
				});
				this.$request.post('/user/login', {
						data: {
							phone: this.phoneData,
							password: this.passData
						}
					})
					.then(res => {
						//简单验证下登录（不安全）
						if (res.data.code == 10000) {
							let authData = {
								...res.data.data.list
							};
							uni.setStorageSync('authData', authData);
							uni.showToast({
								icon: 'success',
								position: 'bottom',
								title: '登录成功'
							});
							setTimeout(() => {
								uni.navigateBack({
									delta: 1
								})
							}, 500)
						} else {
							_this.passData = ""
							uni.showToast({
								position: 'bottom',
								title: res.data.msg
							});
						}
						uni.hideLoading();
					}).catch(err => {
						uni.hideLoading();
						uni.showToast({
							position: 'bottom',
							title: '登录失败'
						});
					})

			},
			login_weixin() {
				//微信登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			login_weibo() {
				//微博登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			login_github() {
				//github登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			}
		}
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
</style>
